//
//  FavoriteViewController.m
//  Cook
//
//  Created by 0.0 on 15-4-20.
//  Copyright (c) 2015年 dingjun. All rights reserved.
//

#import "FavoriteViewController.h"
#import "FavoriteDataBase.h"
#import "FavoriteModel.h"
#import "FavoriteCollectionViewCell.h"
#import "DetailController.h"
#import "VegetableMenu.h"

/// 我的收藏结果界面
@interface FavoriteViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
{
    UICollectionView *_collectionView;
    NSTimer *_timer;
    UIAlertView *_alertView;
}

@property (nonatomic, retain) NSMutableArray *favoriteArray; // 从数据查询到得结果

@end

@implementation FavoriteViewController

- (void)dealloc
{
    [_favoriteArray release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.favoriteArray = [NSMutableArray array];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatBackground];
    [self createCollectionView];
    self.navigationItem.title = @"我的收藏";
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

- (void)creatBackground
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];
    [imageView setImage:[UIImage imageNamed:@"fourthBarBacground.jpg"]];
    [self.view addSubview:imageView];
    [imageView release];
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *myLayout = [[UICollectionViewFlowLayout alloc] init];
    myLayout.itemSize = CGSizeMake((self.view.frame.size.width - 4 * 10) / 3, (self.view.frame.size.width - 4 * 10) / 3);
    myLayout.minimumInteritemSpacing = 10.0f;
    myLayout.minimumLineSpacing = 35.0f;
    myLayout.sectionInset = UIEdgeInsetsMake(10, 0, 64, 20);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) collectionViewLayout:myLayout];
    [myLayout release];
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerClass:[FavoriteCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
    
    
    [_collectionView setDelegate:self];
    [_collectionView setDataSource:self];
    
    [self.view addSubview:_collectionView];
    [_collectionView release];
    
}

- (NSArray *)getFromDataBase
{
    NSArray *array = [FavoriteDataBase searchNovel];
    [self.favoriteArray removeAllObjects];
    [self.favoriteArray addObjectsFromArray:array];
    [_collectionView reloadData];
    
    return array;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.favoriteArray count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FavoriteCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    FavoriteModel *model = [self.favoriteArray objectAtIndex:indexPath.item];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"imagePlace" ofType:@"jpg"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    [cell.favoriteImageView sd_setImageWithURL:[NSURL URLWithString:model.imageURL] placeholderImage:image];
    [cell.favoriteNameLabel setText:model.vegetableName];
    [cell.favoriteNameLabel setTextColor:[UIColor whiteColor]];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    DetailController *detail = [[DetailController alloc] init];
    FavoriteModel *model = [self.favoriteArray objectAtIndex:indexPath.item];
    detail.vegetableMenuModel.vegetable_id = model.vegetabelId;
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSArray *array = [self getFromDataBase];
    if (0 == [array count]) {
        _alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"当前没有收藏内容" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [_alertView show];
        _timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(alertAction2) userInfo:nil repeats:YES];
        
    }
}

- (void)alertAction2
{
    [_timer invalidate];
    [_alertView dismissWithClickedButtonIndex:0 animated:YES];
    [_alertView release];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
